package com.soft.servlet;

import com.soft.dao.UserInfoDao;
import com.soft.entity.UserInfo;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "UserLoginServlet", urlPatterns = "/UserLogin")
public class UserLoginServlet extends HttpServlet {

    private Domain domain;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("GBK");

        domain = new Domain();

        String account = request.getParameter("account");
        String pwd = request.getParameter("password");

        UserInfo ui = domain.getUserInfo(account);
        if (ui != null) {
            if (ui.getStatus() == UserInfo.STATUS.STATUS_FREEZE) {
                request.getSession().setAttribute("flag", 37);
                response.sendRedirect("/denglu.jsp");
                return;
            }
        }

        if(domain.UserLogin(account, pwd)) {
            request.getSession().setAttribute("userAccount", account);
            request.getSession().setAttribute("user", domain.getUserInfo(account));
            request.getSession().setAttribute("flag", 1);
            request.getRequestDispatcher("/GoodsShow").forward(request, response);
        } else {
            request.getSession().setAttribute("flag", 2);
            response.sendRedirect("/denglu.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request,response);
    }
}
